<template>
  <section>
    <!--新增和修改的模态框-->
    <el-dialog title="修改" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-form-item label="员工姓名" prop="username">
          <el-input v-model="addForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工电话" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="员工年龄" prop="age" class="block">
            <el-slider v-model="addForm.age" show-input> </el-slider>
        </el-form-item>
        <el-form-item label="员工邮箱" prop="email" >
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="员工密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="启用状态">
          <el-radio-group v-model="addForm.state">
            <el-radio class="radio" :label="0">启用</el-radio>
            <el-radio class="radio" :label="-1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="listLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="seach">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="employee" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="员工姓名"  sortable>
      </el-table-column>
      <el-table-column prop="email" label="员工邮箱"  sortable>
      </el-table-column>
      <el-table-column prop="phone" label="员工电话" sortable>
      </el-table-column>
      <el-table-column prop="salt" label="员工盐值"  sortable>
      </el-table-column>
      <el-table-column prop="password" label="员工密码"  sortable>
      </el-table-column>
      <el-table-column prop="age" label="员工年龄" sortable>
      </el-table-column>
      <el-table-column prop="state" label="员工状态"  :formatter="formatSex" sortable>
      </el-table-column>
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination @size-change="handleSizeChange" layout="sizes,prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

  </section>
</template>


<script>


export default{
  data() {
    return {
      //列表数据
      employee: [],
      //加载动画
      listLoading: false,
      //勾选
      sels: [],
      //分页
      pageSize: 3,
      currentPage: 1,
      total: 0,
      filters:{
        name:''
      },
      //新增的规则
      addFormRules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      //新增的字段和默认值
      addForm:{
        username: '',
        phone: +86,
        age: 0,
        email: '',
        password: '',
        state: 0,
      },
      //模态框的默认状态
      addFormVisible:false,
    }
  },
  methods: {

    //新增和修改的提交方法
    addSubmit(){
      this.$refs.addForm.validate((valid) => {
          this.$http.put("/emp",this.addForm).then((rew)=>{
            let {message,success} = rew.data;
            //如果成功则xx
            if(success){
              this.$message({
                message: message,
                type: "success",
              })
              //失败则xx
            }else {
              this.$message({
                message: "删除失败了哦",
                type: "error",
              })
            }
            this.addFormVisible=false;
            this.lostEmpData();
          })
      })
    },
    //新增添加的方法
    handleAdd(){
      this.addFormVisible=true;
      this.title="新增";
      for (var i in this.addForm){
        this.addForm[i]='';
        this.addForm.state=0;
      }
    },

    //加载列表的数据
    lostEmpData() {
      let param = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keywords: this.filters.name,
      }
      //发送axios请求   响应值  ,param)
      this.$http.post("/emp", param).then((res) => {

        this.employee = res.data.data;
        this.total = res.data.total;
      })
    },
    //勾选框
    selsChange: function (sels) {
        this.sels= sels;
    },
    /*性别的三目运算*/
    formatSex: function (row, column) {
      return row.state == 1 ? '启用' : row.state == 0 ? '禁用' : '未知';
    },
    //批量删除
    batchRemove: function (){
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中的这些记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.$http.patch("/emp",ids).then((rew)=>{
          //解构表达式
          let {message,success} = rew.data;
          //如果成功则xx
          if(success){
            this.$message({
              message: message,
              type: "success",
            })
            //失败则xx
          }else {
            this.$message({
              message: "删除失败了哦",
              type: "error",
            })
          }
          //不管成功还是失败都要重新加载数据
          this.lostEmpData();
        })
      })
    },
    //编辑功能
    /*编辑*/
    handleEdit: function (index, row) {
      //回显数据
      for( var i in this.addForm){
        this.addForm[i]=row[i];
      }
      //设置id属性
      this.addForm.id=row.id;
      //显示弹出框
      this.addFormVisible=true;
      this.addForm.state=row.state;
    },
    //删除功能
    handleDel: function (index,row) {
      //点击删除询问用户是否真的删除
      this.$confirm('确认删除该记录吗?', '提示', {

      }).then((rew)=>{
        //向后台发送删除的请求 请求类型delete  请求地址/emp  请求附加的参数row.id
          this.$http.delete("/emp/"+row.id).then((rew)=>{
            //解构表达式
              let {message,success} = rew.data;
              //如果成功则xx
              if(success){
                  this.$message({
                    message: message,
                    type: "success",
                  })
                //失败则xx
              }else {
                this.$message({
                  message: "删除失败了哦",
                  type: "error",
                })
              }
              //不管成功还是失败都要重新加载数据
              this.lostEmpData();
          })
      })
    },
    seach() {
      //把当前页更改为第一页
      this.currentPage = 1;
      //重新加载
      this.lostEmpData();
    },
    //页码改变时的操作
    handleSizeChange(val){
      this.pageSize=val;
      this.lostEmpData();
    },
    /*监听分页的操作*/
    handleCurrentChange(val){
      this.currentPage=val;
      this.lostEmpData();
    },


  },
  //钩子方法
  mounted() {
    this.lostEmpData();
  }

}

</script>

